$(document).ready(function(){
    $.fn.dataTableExt.sErrMode = 'throw';
    getAllUsulan()
    get_daftar_pegawai();
    getLevel(); 

    $('#myTab a:first').tab('show');
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      e.target; // activated tab
      e.relatedTarget; // previous tab
    });


    $('#proses_btn').click(function(e){
        e.preventDefault();

        var details= new Array();
        $('#selectPeg:checked').each(function () {
            var id = $(this).closest('tr').attr('detailId');
            details.push(id);
            
        });
        if(details.length<=0){
            alert('Pilih dahulu pegawai');
        }else{
            var e = document.getElementById('aksi_proses');
            var id_aksi = e.options[e.selectedIndex].value;
            //alert(id_aksi+" "+details);

            var all_details=details.join(',');
            $.post('controller/cont.proses.php',{aksi_proses_peg:true,id_aksi:id_aksi,details:all_details},function(data){
                if(data.msg){
                    $('#success').html('Proses Berhasil Dilakukan').fadeIn(500).delay(2500).fadeOut(500);
                }else{
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
                get_daftar_pegawai();
            },'json');
        }
        
    });

    $('#proses_usulan_btn').click(function(e){
        e.preventDefault();

        var usulans= new Array();
        $('#selectUsul:checked').each(function () {
            var id = $(this).closest('tr').attr('usulanId');
            usulans.push(id);
            
        });
        if(usulans.length<=0){
            alert('Pilih dahulu Usulan');
        }else{
            var e = document.getElementById('aksi_proses_usulan');
            var id_aksi_usulan = e.options[e.selectedIndex].value;
            var all_usulan=usulans.join(',');
            $.post('controller/cont.proses.php',{aksi_setujui_usulan:true,id_aksi:id_aksi_usulan,usulans:all_usulan},function(data){
                if(data.msg){
                    $('#success').html('Proses Berhasil Dilakukan').fadeIn(500).delay(2500).fadeOut(500);
                }else{
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
                getAllUsulan();
                get_daftar_pegawai();
            },'json');
        }
        
    });

    $('#proses_lampiran_btn').click(function(e){
        e.preventDefault();

        var details= new Array();
        $('#selectPeg:checked').each(function () {
            var id = $(this).closest('tr').attr('detailId');
            details.push(id);
            
        });

        if(details.length<=0){
            alert('Pilih dahulu pegawai');
        }else{
            var all_details=details.join(',');
            $.post('controller/cont.cetak.php', {cetak_surat: true, details:all_details}, function(data) {
                if (data.error === true) {
                    $('#error').html('Gagal Cetak PDF, '+data.msg).fadeIn(500).delay(2500).fadeOut(500);
                } else {
                    //window.open('dokumen/suratpdf.pdf', '_blank', 'fullscreen=yes');
                    $('#container').fadeOut(250);
                    var pdf = new PDFObject({
                        url: data,
                        id: "pdf",
                        height: "575px",
                        pdfOpenParams: {
                            view: "FitH"
                        }
                    }).embed("pdf");
                }
            }, 'json');
        }
    });

    // $('#proses_doc_btn').click(function(e){
    //     e.preventDefault();

    //     var details= new Array();
    //     $('#selectPeg:checked').each(function () {
    //         var id = $(this).closest('tr').attr('detailId');
    //         details.push(id);
            
    //     });

    //     if(details.length<=0){
    //         alert('Pilih dahulu pegawai');
    //     }else if(details.length>=2){
    //         alert('Hanya Bisa Satu Orang Pegawai');
    //     }else{
    //         var all_details=details.join(',');
    //         $.post('controller/cont.cetak.php', {cetak_surat_doc2: true, details:all_details}, function(data) {
                
    //         });
    //     }
    // });

    $('#proses_xlx_btn').click(function(e){
        e.preventDefault();

        var details= new Array();
        $('#selectPeg:checked').each(function () {
            var id = $(this).closest('tr').attr('detailId');
            details.push(id);
            
        });

        if(details.length<=0){
            alert('Pilih dahulu pegawai');
        }else{
            var all_details=details.join(',');
            $.post('controller/cont.cetak.php', {cetak_xlx: true, details:all_details}, function(data) {
                if (data.error === true) {
                    $('#error').html('Gagal Cetak PDF, '+data.msg).fadeIn(500).delay(2500).fadeOut(500);
                } else {
                    window.open('dokumen/ijinLN.xls', '_blank', 'fullscreen=yes');

                }
            }, 'json');
        }
    });


    $('#jenis_dokumen').on('change',function(e){
        var e = document.getElementById('jenis_dokumen');
        var jenis_dokumen = e.options[e.selectedIndex].value;
        getListUsulan(jenis_dokumen);
        $('#daftarnya').fadeIn(500);
        if(jenis_dokumen===''){
            $('#daftarnya').fadeOut(500);
        }

    });

    $('#btn_upload_proses').click(function(e){
        
        var details= new Array();
        $('#selectPeg2:checked').each(function () {
            var id = $(this).closest('tr').attr('detailId2');
            details.push(id);
            
        });
        var all_details=details.join(',');

        var ef = document.getElementById('jenis_dokumen');
        var jenis_dokumen = ef.options[ef.selectedIndex].value;

        if(details.length<=0){
            alert('Pilih dahulu pegawai');
        }else{
            $.ajaxFileUpload({
                url: 'controller/cont.upload_dokumen.php',
                secureuri: false,
                fileElementId: 'file_proses',
                dataType: 'json',
                data: {id_det_usulan: all_details,id_dok:jenis_dokumen},
                success: function(data, status){
                    if(data.msg){
                        $('#success').html('Upload berhasil dilakukan').fadeIn(500).delay(2500).fadeOut(500);
                        getListUsulan(jenis_dokumen);
                    }else{
                        $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                    }
                }
            });
        }
        e.preventDefault();
    });

    return false;
    
});

function uploadProses(){
    var details= new Array();
        $('#selectPeg2:checked').each(function () {
            var id = $(this).closest('tr').attr('detailId2');
            details.push(id);
            
        });
        
        if(details.length<=0){
            alert('Pilih dahulu pegawai');
        }else{
            var all_details=details.join(',');
            $.ajaxFileUpload({
                url: 'controller/cont.upload_dokumen.php',
                secureuri: false,
                fileElementId: 'file_dokumen_proses',
                dataType: 'json',
                data: {id_det_usulan: all_details},
                success: function(data, status){
                    if(data.msg){
                        $('#success').html('Upload berhasil dilakukan').fadeIn(500).delay(2500).fadeOut(500);
                    }else{
                        $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                    }
                }
            });
        }
        return false;
}

function getListUsulan(jenis_dokumen) {
    $('#loader').fadeIn(500);
    $.getJSON('controller/cont.proses.php', {aksi_daftar_pegawai_filter: true,jen_dok:jenis_dokumen}, function(data) {
        $('#list_pegawai_filter tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.daftar, function(index, daftar) {
                tab = "<tr detailId2=\""+ daftar.id_det_usulan +"\">";
                tab += "<td>" + daftar.nip + "</td>";
                tab += "<td>"+daftar.nama_peg+"</td>";
                tab += "<td>" + daftar.id_tujuan + "</td>";
                tab += "<td>" + daftar.tgl_mulai + " s.d " + daftar.tgl_selesai + "</td>";
                tab += "<td>"+daftar.level+"</td>";
                // tab += "<td align=\"center\"><form role=\"form\ class=\"form-inline\">";
                // tab += "<div class=\"col-lg-9\"><input type=\"hidden\" id=\"id_det\" value=\""+daftar.id_det_usulan+"\"></div>";
                // tab += "<div class=\"col-lg-9\"><input type=\"hidden\" id=\"id_dok\" value=\""+jenis_dokumen+"\"></div>";
                // tab += "<div class=\"col-lg-9\"><input type=\"file\" id=\"file_dokumen\" class=\"form-control\" name=\"file_dokumen\"></div>";
                // tab += "<button type=\"submit\" class=\"btn btn-default btn-sm\" id=\"btn_upload_dokumen\" onclick=\"uploadDokumen()\">Simpan</button>";
                
                    tab+="<td align=\"center\"><input id=\"selectPeg2\" name=\"selectPeg2\" type=\"checkbox\" /></td>";
                
                
                tab += "</tr>";
                //alert(tab);
                $('#list_pegawai_filter tbody').append(tab);
            });

        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_pegawai_filter tbody').append(tab);
        }

        $('#list_pegawai_filter').dataTable({
            "sPaginationType": "bs_full",
            "bInfo": false,
            "bLengthChange": false,
            "iDisplayLength": 5,
          }); 
          $('#list_pegawai_filter').each(function(){
            var datatable = $(this);
            // SEARCH - Add the placeholder for Search and Turn this into in-line form control
            var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
            search_input.attr('placeholder', 'Search');
            search_input.addClass('form-control input-sm');
            // LENGTH - Inline-Form control
            var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
            length_sel.addClass('form-control input-sm');
          });
    });
    $('#loader').fadeOut(500);
}

function get_daftar_pegawai() {
    $.getJSON('controller/cont.proses.php', {aksi_daftar_pegawai: true}, function(data) {
        $('#list_pegawai tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.daftar, function(index, daftar) {
                tab = "<tr detailId=\""+ daftar.id_det_usulan +"\">";
                tab += "<td>" + daftar.nip + "</td>";
                tab += "<td>"+daftar.nama_peg+"</td>";
                tab += "<td>" + daftar.id_cuti + "</td>";
                tab += "<td>" + daftar.id_tujuan + "</td>";
                tab += "<td>" + daftar.tgl_mulai + " s.d " + daftar.tgl_selesai + "</td>";
                tab += "<td>"+daftar.level+"</td>";
                if(daftar.stat==='6' || daftar.stat==='5'){
                    tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"get_dokumen_pegawai(" + daftar.id_det_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";
                }else{
                    tab += "<td align=\"center\"><fieldset disabled><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"get_dokumen(" + daftar.id_det_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></fieldset></td>";
                }
                if(daftar.stat==='6'){
                    tab += "<td align=\"center\"><fieldset disabled><input id=\"selectPeg\" name=\"selectPeg\" type=\"checkbox\" /></fieldset></td>";
                }else{
                    tab += "<td align=\"center\"><input id=\"selectPeg\" name=\"selectPeg\" type=\"checkbox\" /></td>";
                }                
                tab += "</tr>";
                //alert(tab);
                $('#list_pegawai tbody').append(tab);

            });

            $('#list_pegawai').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
              }); 
              $('#list_pegawai').each(function(){
                var datatable = $(this);
                // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                search_input.attr('placeholder', 'Search');
                search_input.addClass('form-control input-sm');
                // LENGTH - Inline-Form control
                var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                length_sel.addClass('form-control input-sm');
              });
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_pegawai tbody').append(tab);
        }
    });
}

function getAllUsulan() {
    $('#loader').fadeIn(500);
    $.getJSON('controller/cont.proses.php', {list_usulan: true}, function(data) {
        $('#list_usulan tbody').html('');
        if(jQuery.isEmptyObject(data)){
            $('#list_usulan tbody').append('<td colspan="9">Data Kosong</td>');  
        }else{
            $.each(data, function(index, data) {
                var tab = "<tr usulanId=\""+ data.id_usulan +"\">";
                tab += "<td>" + data.no_surat + "</td>";
                tab += "<td>" + data.tgl_surat + "</td>";
                tab += "<td>" + data.jml + "</td>";
                //tab += "<td>" + data.kanwil + "</td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"get_dokumen(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";
                if(data.level==='1' || data.level==='2' || data.level==='3' ||data.level==='7'){
                    tab += "<td align=\"center\"><input id=\"selectUsul\" name=\"selectUsul\" type=\"checkbox\" /></td>";
                }else{
                    tab += "<td align=\"center\"><input id=\"selectUsul\" name=\"selectUsul\" type=\"checkbox\" disabled/></td>";
                }
                tab += "</tr>";
                //alert(tab);
                $('#list_usulan tbody').append(tab);
            });

        }
        $('#list_usulan').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
          }); 
          $('#list_usulan').each(function(){
            var datatable = $(this);
            // SEARCH - Add the placeholder for Search and Turn this into in-line form control
            var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
            search_input.attr('placeholder', 'Search');
            search_input.addClass('form-control input-sm');
            // LENGTH - Inline-Form control
            var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
            length_sel.addClass('form-control input-sm');
          });

    });
$('#loader').fadeOut(500);
}

function get_dokumen(id_usulan){
    $.getJSON('controller/cont.dokumen.php',{aksi_list_dokumen:true,id_usulan:id_usulan},function(data){
        $('#list_dokumen tbody').html('');
        var tab='';
        var i=1;
        if(data.msg){
            $.each(data.list_file,function(index,list_file){
                tab+="<tr>";
                tab+="<td>"+i+"</td>";
                tab+="<td>"+list_file.judul+"</td>";
                // tab += "<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-sm\" onclick=\"hapusFile("+list_file.id_dokumen+")\" ><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" onclick=\"download_file("+list_file.id_dokumen+")\"><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Lihat</a></td>";
                tab+="</tr>";
                i++;
            });
            $('#list_dokumen tbody').append(tab);
        }else{
            tab="<tr><td colspan=\"3\">Dokumen Kosong, Silahkan Upload File Anda</td></tr>";
            $('#list_dokumen tbody').append(tab);
        }
    });
}

function get_dokumen_pegawai(id_det_usulan){
    $.getJSON('controller/cont.dokumen.php',{aksi_list_dokumen_peg:true,id_det_usulan:id_det_usulan},function(data){
        $('#list_dokumen tbody').html('');
        var tab='';
        var i=1;
        if(data.msg){
            $.each(data.list_file,function(index,list_file){
                tab+="<tr>";
                tab+="<td>"+i+"</td>";
                tab+="<td>"+list_file.judul+"</td>";
                // tab += "<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-sm\" onclick=\"hapusFile("+list_file.id_dokumen+")\" ><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" onclick=\"download_file("+list_file.id_dokumen+")\"><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Lihat</a></td>";
                tab+="</tr>";
                i++;
            });
            $('#list_dokumen tbody').append(tab);
        }else{
            tab="<tr><td colspan=\"3\">Dokumen Kosong, Silahkan Upload File Anda</td></tr>";
            $('#list_dokumen tbody').append(tab);
        }
    });
}

function getLevel(){
    $.getJSON('controller/cont.proses.php',{aksi_get_all_level2:true},function(data){
        $.each(data, function(index,data){
            var option = $('<option />');
            option.attr('value', data.id_level).text(data.nama_level); 
            $('#aksi_proses').append(option);
        });
    });
    $.getJSON('controller/cont.proses.php',{aksi_get_all_level:true},function(data){
        $.each(data, function(index,data){
            var option = $('<option />');
            option.attr('value', data.id_level).text(data.nama_level); 
            $('#aksi_proses_usulan').append(option);
        });
    });
}